Extensiones a Shiny

htmlwidgets: Un marco para embeber visualizaciones de JavaScript en R

Hèctor Perpiñán Fabuel

Servei d’Eficiencia i Anàlisi Econòmic - Conselleria de Sanitat Universal i Salut Pública

Máster en Bioestadística (Universitat de València) - 14 de diciembre de 2022

Extensiones a Shiny (mediante paquetes)

htmlwidgets

127 widgets registrados actualmente (selección):

  • leaflet: Mapeado geoespacial interactivo

  • plotly: Creación de gráficos interactivos

  • dygraphs: Graficado de series temporales

  • DataTables: Visualización de datos tabulares

  • d3heatmap: Mapas de calor

  • networkD3: Visualización gráfica de datos con D3

  • DiagrammeR: Grafos y diagramas de flujo

  • rglwidget: Renderiza escenas creadas con rgl

Leaflet: mapas interactivos

  • Llamadas en Shiny: renderLeaflet() y leafletOutput()
  • Tiles
library(leaflet)
leaflet() |>  
  addTiles() |> 
  addMarkers(lng = -0.420664, lat = 39.508154, popup = "Facultat Matemàtiques (UV)")

Plotly: Navaja suiza de gráficos interactivos

  • Llamadas en Shiny: renderPlotly() y plotlyOutput()
library(ggplot2); library(plotly)
p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) + geom_bar(position = "dodge")
ggplotly(p)

dygraphs: Gráficos interactivos para series temporales

  • Llamadas en Shiny: renderDygraph() y dygraphOutput()
library(dygraphs)
lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") |> 
  dyHighlight(highlightCircleSize = 5, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE)

DataTable: Tablas dinámicas (paquete DT)

  • Llamadas en Shiny: renderDataTable() y dataTableOutput()
DT::datatable(iris, options = list(pageLength = 6, dom = "pt", 
                                   language = list(url = '//cdn.datatables.net/plug-ins/1.10.11/i18n/Spanish.json')),
              rownames = FALSE, escape = FALSE)

d3heatmap: Mapas de calor

  • Llamadas en Shiny: renderD3heatmap() y d3heatmapOutput()
library(d3heatmap)
d3heatmap(mtcars, scale = "column", colors = "Spectral")

networkD3: Grafos interactivos

  • Llamadas en Shiny: renderForceNetwork() y forceNetworkOutput()
library(networkD3)
data(MisLinks, MisNodes)
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source", 
             Target = "target", Value = "value", NodeID = "name", Group = "group", opacity = 0.4)

DiagrammeR: Grafos y diagramas de flujo

library(DiagrammeR)
grViz("
digraph boxes_and_circles {

node [shape = polygon
      sides = 6
      fontname = Helvetica
      penwidth = 2.0
      width = 2.0]
ICTUS1 [label = 'ICTUS\nBarcelona']; ICTUS2 [label = 'ICTUS\nLleida']; ICTUS3 [label = 'ICTUS\nBellvitge']; 

node [shape = circle
      fontname = Helvetica
      penwidth = 2.0
      width = 1.0]
Aleatorización [color = pink, style = filled]

node [shape = box]
Barcelona; Bellvitge; Lleida; SEM;Intervención; Control

node [shape = ellipse]
'RACE 1-3'; 'RACE 4-6'; 'RACE 7-9';

ICTUS1 -> SEM; ICTUS2 -> SEM; ICTUS3 -> SEM;
SEM -> 'RACE 1-3'; SEM -> 'RACE 4-6'; SEM -> 'RACE 7-9' [label = 'ambulancia'];
'RACE 1-3' -> SEM; 'RACE 4-6' -> SEM; 'RACE 7-9' -> SEM;
SEM -> Aleatorización;
Aleatorización -> Intervención; Aleatorización -> Control;
Intervención -> Barcelona; Intervención -> Bellvitge; Intervención -> Lleida;
Control -> Barcelona; Control -> Bellvitge; Control -> Lleida;

graph [nodesep = 0.1]

}
", height = 500)

rglwidget

library(rgl); library(rglwidget); library(htmltools)

theta <- seq(0, 6*pi, len = 100)
xyz <- cbind(sin(theta), cos(theta), theta)
lineid <- plot3d(xyz, type = "l", alpha = 1:0, lwd = 5, 
                 col = "blue")["data"]

browsable(tagList(
  rglwidget(elementId = "example", width = 500, 
            height = 400, controllers = "player"),
  playwidget("example", ageControl(births = theta, 
                                   ages = c(0, 0, 1), 
                                   objids = lineid, 
                                   alpha = c(0, 1, 0)), 
             start = 1, stop = 6*pi, 
             step = 0.1, rate = 6,
             elementId = "player")))

y muchos más en http://gallery.htmlwidgets.org/

y extensiones con crosstalk.

MUCHAS GRACIAS


Contacto: perpinan_hec@gva.es